﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>产品应用</title>
    <script src="/Apm/js/CMSEdit.js"></script>
    <style>
        #ContentData{
            height:25rem;
        }
        .upload-div {
            display: flex;
            flex-direction: column;
            margin: 1rem 2rem;
            border: 1px solid #eee;
        }

        .upload-label {
            font-size: 1rem;
            padding: 0.8rem;
            background-color: #FBFBFB;
            border-bottom: 1px solid #eee;
        }

        .upload-image {
            width: 6rem;
            height: 6rem;
            padding: 1rem;
        }
    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">

        </div>
        
        <div class="layui-form layui-form-pane edit-form">
            
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input id="Title" type="text" name="Title" required lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    <textarea id="ContentData" name="ContentData" placeholder="请输入内容" class="layui-textarea"></textarea>
                </div>
            </div>
            
            
            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>
        <div class="upload-div">
            <label class="upload-label">图片</label>
            <img id="UploadImage" class="upload-image" src="/image//upload.png" />
        </div>
        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script>
    layui.use(['layer', 'form', 'upload'], function () {
        var layer = layui.layer,
            form = layui.form,
            upload = layui.upload;

        var type = MyPublic.getUrlParam('type');
        var id = MyPublic.getUrlParam('id');

        switch (type) {
            case 'add':
                $("#title").html('添加');

                break;
            case 'edit':
                $("#title").html('编辑');
                BindDataModel(id)
                break;
        }
        
        $("#truebutton").click(function () {
            $("#submit").click();
        })

        //上传单张图片
        upload.render({
            elem: '#UploadImage',
            url: ApiURL + 'api/Upload/OneImagePublic?Token=' + MyPublic.getToken(),
            accept: 'images',
            exts: 'jpg|png|gif|bmp|jpeg',
            done: function (res) {
                if (res.code === "0") {
                    var imageurl = res.data.Url;
                    $("#UploadImage").attr('src', ApiURL + imageurl);
                    $("#UploadImage").attr('data-url', imageurl);
                }
                else {
                    layer.msg("上传失败");
                }
            },
            error: function () {
                layer.confirm('登陆凭证已过期，请重新登陆！', { icon: 2, title: '警告' }, function (index) {

                    window.open("/login.html");
                    localStorage.clear();//清空存储的值
                    sessionStorage.clear();
                    layer.close(index);
                });
            }
        })

        //添加或修改
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var json = data.field;
            var image = $("#UploadImage").attr('data-url');
            json.ImageUrl = image;
            
            switch (type) {
                case 'add':
                    ajaxpost({
                        url: 'api/NBtuoda/ApplicationCreate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10000") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })

                    break;
                case 'edit':
                    json.Guid = id;
                    ajaxpost({
                        url: 'api/NBtuoda/ApplicationUpdate?Token=' + MyPublic.getToken(),
                        data: json,
                        success: function (data) {
                            data = JSON.parse(data);
                            if (data.code === "10001") {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                            else {
                                layer.msg(data.msg);
                            }
                        }
                    })
                    break;
            }

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
    
    function BindDataModel(guid) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/NBtuodaPublic/GetApplication?guid=' + guid,
                success: function (data) {
                    $("#Title").val(data.Title);
                    $("#ContentData").val(data.ContentData);
                    //图片
                    if (data.ImageUrl != "") {
                        $("#UploadImage").attr('src', ApiURL + data.ImageUrl);
                        $("#UploadImage").attr('data-url', data.ImageUrl);
                    }
                    
                }
            })
        });
    }
    
</script>